home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Compilers⁄Interps / kevoSource / memory.c < prev    next >
Text File  |  1993-05-11  |  6KB  |  230 lines

  1. /* Kevo -- a prototype-based object-oriented language */
  2. /* (c) Antero Taivalsaari 1991-1993                   */
  3. /* Some parts (c) Antero Taivalsaari 1986-1988           */
  4. /* memory.c: memory and object allocation internals   */
  5.  
  6. #include "global.h"
  7.  
  8. /*--------------------------------------------------------------------------*/
  9. /* Memory allocation internals */
  10.  
  11. char* mymalloc(size)
  12. int size;
  13. {
  14.   char* addr;
  15.  
  16.   if (!(addr = (char*)malloc(size))) {
  17.     SysBeep(1); SysBeep(1);
  18.     fprintf(confile, "== Not enough memory (malloc request: %ld bytes) ==\n", size);
  19.     if (!supervisor) {
  20.         ownPrintf("-- Not enough memory");
  21.         execute((*up)->errorVector); 
  22.     }
  23.     ownLongJmp();
  24.   }
  25.   return addr;
  26. }
  27.  
  28.  
  29. char* mycalloc(nelem, elsize)
  30. int nelem;
  31. int elsize;
  32. {
  33.   char* addr;
  34.  
  35.   if (!(addr = (char*)calloc(nelem, elsize))) {
  36.     SysBeep(1); SysBeep(1);
  37.     fprintf(confile, "== Not enough memory (calloc request: %ld bytes) ==\n", nelem*elsize);
  38.     if (!supervisor) {
  39.         ownPrintf("-- Not enough memory");
  40.         execute((*up)->errorVector); 
  41.     }
  42.     ownLongJmp();
  43.   }
  44.   return addr;
  45. }
  46.  
  47.  
  48. char* myrealloc(ptr, newsize)
  49. char* ptr;
  50. int   newsize;
  51. {
  52.   char* addr;
  53.  
  54.   if (!(addr = (char*)realloc(ptr, newsize))) {
  55.     SysBeep(1); SysBeep(1);
  56.     fprintf(confile, "== Not enough memory (realloc request: %ld bytes) ==\n", newsize);
  57.     if (!supervisor) {
  58.         ownPrintf("-- Not enough memory");
  59.         execute((*up)->errorVector); 
  60.     }
  61.     ownLongJmp();
  62.   }
  63.   return addr;
  64. }
  65.  
  66.  
  67. char* allocStrCpy(source)
  68. char* source;
  69. {
  70.   char* target;
  71.  
  72.   target = (char*)mymalloc(strlen(source)+1);
  73.   return(strcpy(target, source));
  74. }
  75.  
  76.  
  77. /*--------------------------------------------------------------------------*/
  78. /* Object-specific operations */
  79. /*
  80.     These operations create, manipulate, and delete "non-object-oriented"
  81.     objects (handle-store structures).
  82.     
  83.     For further info, see 'memory.h'.
  84. */
  85.  
  86. /* createStore(): create an initialized store part for an object */
  87. /* Size is given in CELLS */
  88.  
  89. /* inlined for speed (see the header file 'memory.h')
  90. STORE* createStore(size)
  91. int size;
  92. {
  93.   return((STORE*)mycalloc(1, size*CELL));
  94. }
  95. */
  96.  
  97.  
  98. /* createPrimitive(): create a primitive object */
  99. /*
  100.     Primitives refer to the C operation directly via their 'mfa'
  101.     field. Unlike other objects, primitives do not have a separate
  102.     store part. The size field 'sfa' is always zero.
  103. */ 
  104.  
  105. OBJECT* createPrimitive(code)
  106. int* code;
  107. {
  108.   OBJECT* newObject = (OBJECT*)mymalloc(sizeof(OBJECT));
  109.   
  110.       newObject->mfa = (STORE*)code;    /* Address of the C function */
  111.       newObject->sfa = 0;    /* The size field of a primitive is zero */
  112.       
  113.       return(newObject);
  114. }
  115.  
  116.  
  117. /* createClosure(): create a closure object */
  118.  
  119. OBJECT* createClosure(size)
  120. int size;    /* how much space do we need (in CELLS) */
  121. {
  122.   /* Allocate object */
  123.   OBJECT* newObject = (OBJECT*)mymalloc(sizeof(OBJECT));
  124.   STORE*  newStore;
  125.  
  126.   /* Ensure that size is at least 1 */
  127.   size = (size > 1) ? size : 1;
  128.  
  129.   /* Store 'exit' to the beginning of the newly allocated memory */
  130.   newStore = createStore(size);
  131.   newStore->efa = (int*)oExit;
  132.  
  133.   /* Set the fields needed for closure objects */
  134.   newObject->mfa = newStore;     /* Storage part for storing code */
  135.   newObject->sfa = size;        /* Size of storage part in cells */
  136.  
  137.   return(newObject);
  138. }
  139.  
  140.  
  141. /* copyObject(): shallow copy an existing object and its store part */
  142. /* this operation is needed for multitasking and prototype-based OOP */
  143.  
  144. OBJECT* copyObject(oldObject)
  145. OBJECT* oldObject;
  146. {
  147.   int size = oldObject->sfa;
  148.   OBJECT* newObject = (OBJECT*)mymalloc(sizeof(OBJECT));
  149.  
  150.     if (size) { /* Object is not a primitive */
  151.       STORE*  newStore = createStore(size);
  152.       int* newp = (int*)newStore;
  153.       int* oldp = (int*)oldObject->mfa;
  154.       int i;
  155.   
  156.         /* Copy old data to the new store part (shallow copy) */
  157.         for (i = 0; i < size; i++) *newp++ = *oldp++;
  158.   
  159.         /* Set the fields needed for the new object */
  160.         newObject->mfa = newStore;     /* Duplicated storage part */
  161.         newObject->sfa = size;        /* Size of storage part in cells */
  162.     }
  163.     else {    /* Object is a primitive */
  164.         newObject->mfa = oldObject->mfa; /* Share the same C operation */
  165.         /* newObject->sfa = 0;    */        /* Size is automatically zero (=primitive) */
  166.     }
  167.  
  168.     return(newObject);
  169. }
  170.  
  171.  
  172. /* deleteObject(): delete an existing object and its store part */
  173.  
  174. void deleteObject(object)
  175. OBJECT* object;
  176. {
  177.     /* if the object has a store part (= is not a primitive), free the store */
  178.     if (object->sfa) free((STORE*)object->mfa);
  179.     free(object);
  180. }
  181.  
  182.  
  183. /* resizeClosure(): resize the storage area of an existing object */
  184.  
  185. void resizeClosure(object, newsize)
  186. OBJECT* object;
  187. int newsize;    /* The new size of the storage area (in cells) */
  188. {
  189.   int oldsize = object->sfa;
  190.  
  191.   /* If the new size is the same as the old size -> do nothing */
  192.  
  193.   /* Primitives (objects with size 0) cannot be resized at all */
  194.   /* (since they do not have a store part) */
  195.  
  196.   /* Since the zero size implies that an object is a primitive */
  197.   /* we do not allow size to be changed to zero */
  198.  
  199.   if (newsize == oldsize || oldsize == 0 || newsize < 1) return;
  200.  
  201.   object->mfa = (STORE*)myrealloc(object->mfa, newsize*CELL);
  202.   object->sfa = newsize;
  203.  
  204.   /* Clear the possible newly allocated extra memory to zeros */
  205.   while (oldsize < newsize) {
  206.     int* ptr = (int*)object->mfa + oldsize++;
  207.     *ptr = 0;
  208.   }
  209. }    
  210.  
  211.  
  212. /* Recognize an OOP object */
  213.  
  214. int recognizeObject(object)
  215. OBJECT* object;
  216. {
  217.   OBJECT* code;
  218.   
  219.     if (!object) return UNKNOWN;
  220.     if (!object->mfa) return UNKNOWN;
  221.     if (object->sfa == 0) return PRIMITIVE;
  222.     
  223.     code = (OBJECT*)object->mfa->efa;
  224.     if (code == oREF) return REF;
  225.     if (code == oVAR) return VAR;
  226.     if (code == oSharedConst) return CONST;
  227.     return METHOD;
  228. }    
  229.  
  230.